package com.cyzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyzy.domain.dto.ResponseData;
import com.cyzy.domain.query.ShowBaseList;
import com.cyzy.domain.vo.TBase;
import com.cyzy.domain.vo.TSeed;
import com.cyzy.mapper.LandMapper;
import com.cyzy.mapper.SeedMapper;
import com.cyzy.service.LandService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LandServiceImpl implements LandService {
    @Autowired
    private LandMapper landMapper;

    @Autowired
    private SeedMapper seedMapper;
    /**
     * 空闲地显示基地
     * @param showBaseList  空闲地显示参数实体类
     * @return 符合的条件的基地 返回前端
     */
    @Override
    public ResponseData showBaseList(ShowBaseList showBaseList) {
        //分页器
        Page<TBase> page = new Page<>(showBaseList.getCurrentPage(),showBaseList.getSize());
        //mybatis-plus  自定义where条件
        QueryWrapper<TBase> wrapper = new QueryWrapper<>();
        wrapper.eq("region_id",showBaseList.getRegionId())
                .eq("base_type",showBaseList.getBaseType());
        if(showBaseList.getInput()!=null  && !showBaseList.getInput().trim().isEmpty()){
            wrapper.like("base_name", showBaseList.getInput());
        }
        //开始分页使用
        IPage<TBase> result = landMapper.selectPage(page,wrapper);

        ResponseData data = new ResponseData();
        data.setData(result.getRecords());
        // 假设result.getTotal()返回的是一个long类型的值
        Integer total = Math.toIntExact(result.getTotal());
        data.setTotal(total);
        return data;
    }
    /**
     * 基地详情页  种子选择
     * @return 返回根据类型的种子(比如蔬菜区,果树区)
     */
    @Override
    public ResponseData showSeedList(String baseType) {
        //mybatisPlus 自定义where条件
        QueryWrapper<TSeed> wrapper = new QueryWrapper<>();
        wrapper.eq("base_type",baseType);
        //调用查询方法
        List<TSeed> list = seedMapper.selectList(wrapper);
        ResponseData data = new ResponseData();
        data.setData(list);
        return data;
    }
}
